//
//  PKMatchView.m
//  Learn
//
//  Created by apple on 2024/4/7.
//  Copyright © 2024 迟建国. All rights reserved.
//

#import "PKMatchView.h"
#import "PkMatchModel.h"
@interface PKMatchView()
/* <#描述#> */
@property (nonatomic, strong) UIImageView    * leftBgImg;
@property (nonatomic, strong) UIImageView    * rightBgImg;
@property (nonatomic, strong) UIImageView    * pkIcon;

@property (nonatomic, strong) UIImageView    * leftUserImg;
@property (nonatomic, strong) UILabel    * leftUserName;
@property (nonatomic, strong) UIImageView    * rightUserImg;
@property (nonatomic, strong) UILabel    * rightUserName;

@property (nonatomic, strong) UILabel    * tipLabel;

@end

@implementation PKMatchView

-(instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createUI];
    }
    return self;
}
- (void)createUI {
    [self addSubview:self.pkIcon];
    [self addSubview:self.leftBgImg];
    [self addSubview:self.rightBgImg];
    [self addSubview:self.tipLabel];
}
- (UIImageView *)pkIcon {
    if(!_pkIcon){
        _pkIcon = [[UIImageView alloc]initWithFrame:CGRectMake(63, 91, 260,322)];
        _pkIcon.contentMode = UIViewContentModeScaleAspectFill;
        _pkIcon.image = [UIImage imageNamed:@"pk_anIcon"];
    }
    return _pkIcon;
}

- (UIImageView *)leftBgImg {
    if(!_leftBgImg){
        _leftBgImg = [[UIImageView alloc]initWithFrame:CGRectMake(-188, 10, 188,106)];
        _leftBgImg.contentMode = UIViewContentModeScaleAspectFill;
        _leftBgImg.image = [UIImage imageNamed:@"pk_left"];
        [_leftBgImg addSubview:self.leftUserImg];
        [_leftBgImg addSubview:self.leftUserName];
    }
    return _leftBgImg;
}
- (UIImageView *)rightBgImg {
    if(!_rightBgImg){
        _rightBgImg = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_W, self.pkIcon.bottom, 188,106)];
        _rightBgImg.contentMode = UIViewContentModeScaleAspectFill;
        _rightBgImg.image = [UIImage imageNamed:@"pk_right"];
        [_rightBgImg addSubview:self.rightUserImg];
        [_rightBgImg addSubview:self.rightUserName];
    }
    return _rightBgImg;
}
- (UILabel *)tipLabel {
    if(!_tipLabel){
        _tipLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 60 + self.rightBgImg.bottom, SCREEN_W, 28)];
        _tipLabel.text = @"等待对方准备…";
        _tipLabel.textColor = [UIColor whiteColor];
        _tipLabel.font = PingFangMediumFont(20);
        _tipLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _tipLabel;
}

- (UIImageView *)leftUserImg {
    if(!_leftUserImg){
        _leftUserImg = [[UIImageView alloc]initWithFrame:CGRectMake(53, 15, 44, 44)];
        _leftUserImg.contentMode = UIViewContentModeScaleAspectFill;
//        _leftUserImg.image = [UIImage imageNamed:@"1"];
        [_leftUserImg js_setRoundedCorners:UIRectCornerAllCorners radii:22];
    }
    return _leftUserImg;
}
- (UILabel *)leftUserName {
    if(!_leftUserName){
        _leftUserName = [[UILabel alloc]initWithFrame:CGRectMake(0, 10+ self.leftUserImg.bottom, 100, 22)];
        _leftUserName.centerX = self.leftUserImg.centerX;
//        _leftUserName.text = @"张小明";
        _leftUserName.textColor = [UIColor blackColor];
        _leftUserName.font = PingFangMediumFont(16);
        _leftUserName.textAlignment = NSTextAlignmentCenter;
    }
    return _leftUserName;
}
- (UIImageView *)rightUserImg {
    if(!_rightUserImg){
        _rightUserImg = [[UIImageView alloc]initWithFrame:CGRectMake(82, 15, 44, 44)];
        _rightUserImg.contentMode = UIViewContentModeScaleAspectFill;
//        _rightUserImg.image = [UIImage imageNamed:@"1"];
        [_rightUserImg js_setRoundedCorners:UIRectCornerAllCorners radii:22];
    }
    return _rightUserImg;
}
- (UILabel *)rightUserName {
    if(!_rightUserName){
        _rightUserName = [[UILabel alloc]initWithFrame:CGRectMake(0, 10 + self.rightUserImg.bottom, 100, 22)];
        _rightUserName.centerX = self.rightUserImg.centerX;
        _rightUserName.textColor = [UIColor blackColor];
        _rightUserName.font = PingFangMediumFont(16);
        _rightUserName.textAlignment = NSTextAlignmentCenter;
    }
    return _rightUserName;
}

- (void)startAnimation {
    self.pkIcon.transform = CGAffineTransformMakeScale(0.8, 0.8);
    [UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
        self.leftBgImg.left = 0;
        self.rightBgImg.right = SCREEN_W;
        self.pkIcon.transform = CGAffineTransformMakeScale(1.2, 1.2);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
            self.pkIcon.transform = CGAffineTransformMakeScale(1.0, 1.0);
        } completion:^(BOOL finished) {
        }];
    }];
    
    
    
}

- (void)updateUIWithModel:(id)model {
    if ([model isKindOfClass:PkMatchModel.class]) {
        PkMatchModel *matchModel = (PkMatchModel *)model;
        
        [self.leftUserImg sd_setImageWithURL:[NSURL URLWithString:lApp.useModel.avatar]placeholderImage:[UIImage imageNamed:@"pk_userHeader"]];
        self.leftUserName.text = lApp.useModel.nickname;
        [self.rightUserImg sd_setImageWithURL:[NSURL URLWithString:matchModel.otherUser.avatar]placeholderImage:[UIImage imageNamed:@"pk_userHeader"]];
        self.rightUserName.text = matchModel.otherUser.nickname;
    }
}
@end
